package com.springboot.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.base.model.Seller;
import com.springboot.base.dal.SellerMapper;
import com.springboot.base.service.ISellerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author meiguangya
 * @since 2020-07-17
 */
@Service
public class SellerServiceImpl extends ServiceImpl<SellerMapper, Seller> implements ISellerService {

    @Autowired
    private SellerMapper sellerMapper;

    @Override
    public IPage<Seller> qrySellerList(Page<Seller> page) {
        return sellerMapper.selectListByPage(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Seller create(Seller sellerModel) {
        sellerModel.setCreatedAt(new Date());
        sellerModel.setUpdatedAt(new Date());
        sellerModel.setRemarkScore(new BigDecimal(0));
        sellerModel.setDisabledFlag(0);

        save(sellerModel);
        return getById(sellerModel.getId());
    }

    @Override
    public Seller changeStatus(Integer id, int i) {
        Seller s = sellerMapper.selectById(id);
        s.setDisabledFlag(i);
        s.setUpdatedAt(new Date());
        sellerMapper.updateById(s);
        return sellerMapper.selectById(id);
    }

    @Override
    public int countAllSeller() {
        return sellerMapper.selectCount(new QueryWrapper<Seller>().eq("disabled_flag",0));
    }


}
